// Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
// Distributable under LGPL license. See terms of license at gnu.org.

package nl.justobjects.pushlet.core;

import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

/**
 * Maintains lifecycle of event sources.
 *
 * @version $Id: EventSourceManager.java,v 1.4 2003/08/15 08:37:40 justb Exp $
 * @author Just van den Broecke - Just Objects &copy;
 **/
public class EventSourceManager {
	private static Vector eventSources = null;

	/**
	 * Initialize event sources from properties file.
	 */
	public static void init() {
		// Load Event sources using properties file.
        Log.info("EventSourceManager init");

		Properties properties = new Properties();

		// Use the class loader that loaded our class.
		// This is required where for reasons like security
		// multiple class loaders exist, e.g. BEA WebLogic.
		// Thanks to Lutz Lennemann 29-aug-2000.
		ClassLoader classLoader = EventSourceManager.class.getClassLoader();

		// File path to be found through (our own) classloader.
		String propertiesFilePath = null;

		try {
			// First try simple name
			propertiesFilePath = "eventsources.properties";

			// Try loading it.
			properties.load(classLoader.getResourceAsStream(propertiesFilePath));
		}
		catch (Throwable ex1) {
			Log.warn("Cannot find event generator properties file: " + propertiesFilePath, ex1);

			// Do another try with a full package name.
			propertiesFilePath = "nl/justobjects/pushlet/core/eventsources.properties";

			Log.info("Will now try finding: " + propertiesFilePath);
			try {
				// Try loading it.
				properties.load(classLoader.getResourceAsStream(propertiesFilePath));
				Log.info("OK loaded: " + propertiesFilePath);
			}
			catch (Throwable ex2) {
				Log.warn("Still cannot load event generator properties file. Is it in your classpath ? (Ok if you don't use it)", ex2);
			}
		}

		// Create event source collection + one extra for Probe event source
		eventSources = new Vector(properties.size() + 1);

		// Always add the prober (needed to ping client connection).
		eventSources.addElement(new ProbeEventPullSource());

		// Add the configured sources
		for (Enumeration e = properties.keys(); e.hasMoreElements();) {
			String nextKey = (String) e.nextElement();
			String nextClass = properties.getProperty(nextKey);
			EventSource nextEventSource = null;
			try {
				nextEventSource = (EventSource) Class.forName(nextClass).newInstance();
				Log.info("created EventSource: key=" + nextKey + " class=" + nextClass);
				eventSources.addElement(nextEventSource);
			}
			catch (Exception ex) {
				Log.warn("Cannot create EventSource: class=" + nextClass, ex);
			}
		}
	}

	/** Activate all event sources. */
	public static void activate() {
		for (int i = 0; i < eventSources.size(); i++) {
			((EventSource) eventSources.elementAt(i)).activate();
		}
	}

	/** Deactivate all event sources. */
	public static void passivate() {
		for (int i = 0; i < eventSources.size(); i++) {
			((EventSource) eventSources.elementAt(i)).passivate();
		}
	}
}

/*
 * $Log: EventSourceManager.java,v $
 * Revision 1.4  2003/08/15 08:37:40  justb
 * fix/add Copyright+LGPL file headers and footers
 *
 * Revision 1.3  2003/08/12 09:41:35  justb
 * replace static initalizer with explicit init()
 *
 * Revision 1.2  2003/05/18 16:15:08  justb
 * support for XML encoded Events
 *
 * Revision 1.1.1.1  2002/09/24 21:02:31  justb
 * import to sourceforge
 *
 * Revision 1.1.1.1  2002/09/20 22:48:17  justb
 * import to SF
 *
 * Revision 1.1.1.1  2002/09/20 14:19:03  justb
 * first import into SF
 *
 * Revision 1.5  2002/04/15 20:42:41  just
 * reformatting and renaming GuardedQueue to BlockingQueue
 *
 * Revision 1.4  2000/10/30 14:15:47  just
 * no message
 *
 * Revision 1.3  2000/08/31 08:26:54  just
 * Changed classloader that loads eventsources.properties to use EventSourceManager's classloader
 *
 * Revision 1.2  2000/08/21 20:48:29  just
 * added CVS log and id tags plus copyrights
 *
 *
 */
